package com.chanyachting.emall.action.upload;

import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.chanyachting.emall.form.UploadForm;
import com.chanyachting.emall.util.RequestUtil;

public class UploadAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		request.getRequestURI();
		
		  ActionForward af = null;

	      UploadForm uploadForm = (UploadForm) form;

	      FormFile myFile = uploadForm.getFile();

	      if(myFile!=null){

	          try {
	        	  
	        	  String uploadPath=ru.getDefUploadPath();
	        	  
	        	  if(myFile.getContentType().equals("image/gif")){
	        		  
	        		  uploadPath=ru.getImgUploadPath();
	        		  
	        	  }else{
	        		  
	        		  uploadPath=ru.getfileUploadPath();
	        	  }

	             FileOutputStream fos = new FileOutputStream(uploadPath+myFile.getFileName());

	             fos.write(myFile.getFileData());

	             fos.flush();

	             fos.close();

	             af =  mapping.findForward("success");

	          } catch (Exception e) {

	             e.printStackTrace();

	             af =  mapping.findForward("error");

	          }

	      }

	      return af;
	}
	
	 

	
}
